package com.bigdata.demo.web.controller;

import com.bigdata.demo.base.ApiDataTableResponse;
import com.bigdata.demo.base.ApiResponse;
import com.bigdata.demo.service.CRReportService;
import com.bigdata.demo.service.ServiceMultiResult;
import com.bigdata.demo.service.ServiceResult;
import com.bigdata.demo.service.search.ISearchService;
import com.bigdata.demo.web.dto.CRDTO;
import com.bigdata.demo.web.form.CRReportSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Controller
public class CRController {

    @Autowired
    private ISearchService searchService;

    @Autowired
    private CRReportService crReportService;

    @GetMapping("search/kks/autocomplete")
    @ResponseBody
    public ApiResponse autocomplete(@RequestParam(value = "prefix") String prefix) {

        if (prefix.isEmpty()) {
            return ApiResponse.ofStatus(ApiResponse.Status.BAD_REQUEST);
        }
        ServiceResult<List<String>> result = this.searchService.suggest(prefix);
        return ApiResponse.ofSuccess(result.getResult());
    }
    @PostMapping("search/kks")
    @ResponseBody
    public ApiDataTableResponse crsForAPI(@ModelAttribute CRReportSearch  searchBody) {
        ServiceMultiResult<CRDTO> result = searchService.query(searchBody);

        ApiDataTableResponse response = new ApiDataTableResponse(ApiResponse.Status.SUCCESS);
        response.setData(result.getResult());
        response.setRecordsFiltered(result.getTotal());
        response.setRecordsTotal(result.getTotal());

        return response;
    }

}
